package org.honeynet.hbstats.statservlet;

import java.util.Calendar;
import java.util.TimeZone;

public class Util {

    private static TimeZone utc = TimeZone.getTimeZone("UTC");

    public static Calendar getUTCCal(long ts) {
        Calendar cal = Calendar.getInstance(utc);
        cal.setTimeInMillis(ts);
        return cal;
    }

    public static long truncateToMonth(long ts) {
        Calendar cal = getUTCCal(ts);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);

        return cal.getTimeInMillis();
    }

    public static long truncateToMin(long ts) {
        Calendar cal = getUTCCal(ts);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);

        return cal.getTimeInMillis();
    }

}